package com.cictec.middleware.gps.engine.position.handler;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.log.MultiLog;
import com.cictec.middleware.feign.distribute.MiddlewarePositionService;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.BusPositionHandler;
import com.cictec.middleware.gps.engine.service.DataStorageEnterOutService;
import com.cictec.middleware.gps.engine.service.DataStorageToRedisService;
import com.cictec.middleware.gps.engine.utils.MultiLogUtil;

/**
 * 位置信息（转发、存储）处理
 * 
 * @author zhou qi
 *
 */
@Component
public class BusPositionDistributeHandler implements BusPositionHandler {

	private static Logger logger = LoggerFactory.getLogger(BusPositionDistributeHandler.class);
	
	@Autowired
	private DataStorageToRedisService dataStorageToRedisService;
	@Autowired
	private DataStorageEnterOutService dataStorageEnterOutService;
	@Autowired
	private MiddlewarePositionService middlewarePositionService;
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		// TODO Auto-generated method stub
		MultiLog log = MultiLogUtil.getPositionJudgeLog(busInstance);
		
		addBeforHandler(busInstance);
		
		String str = "isdef GPS_DRIFT && GPS_DRIFT==0";
		Serializable compiled = MVEL.compileExpression(str);
		Boolean result = (Boolean) MVEL.executeExpression(compiled, busInstance.getAttributes());

		if (result) {
			log.debug("数据推送：【坐标漂移】");
			return;
		} else {

			// 判定是否偏线
			if (busInstance.isInDeviate()) {
				log.debug("数据推送：【数据偏线】");
				
				dataStorageToRedisService.saveBusInstanceToRedis(busInstance);	// 保存位置信息
				
//				SendHandler.sendBusPostionToMq(busInstance);			// 推送至 mq
//				SendHandler.sendBusPostionToStomp(busInstance);			// 推送至 apollo
//				SendHandler.sendBusPostionToMqPlatform(busInstance);	// 推送至mq 数据转发平台队列
			} else {
				
				log.debug("数据推送：【车辆在线路上的相对位置】");
				// 判断为设备上报的进出站数据
				
				dataStorageToRedisService.saveBusInstanceToRedis(busInstance);	// 保存位置信息
				dataStorageToRedisService.saveEnterOutToRedis(busInstance);		// 保存进出站信息至 redis 【用于里程统计、趟次计算】
				dataStorageEnterOutService.saveEnterOutToES(busInstance);		// 保存进出站信息至 es
				
//				SendHandler.sendBusPostionToMq(busInstance);		// 推送至 mq
//				SendHandler.sendBusPostionToStomp(busInstance);		// 推送至 apollo
//				SendHandler.sendBusPostionToMqPlatform(busInstance);	// 推送至mq 数据转发平台队列
//				SendHandler.sendBusPostionToKafka(busInstance);		// 推送至 kafka
			}
		}
		
		
		Map data = new HashMap();
		data.putAll(busInstance.getAttributes());
		
		
		middlewarePositionService.sendData(data);
	}
	

	

	private void addBeforHandler(BusInstance busInstance) {
		
		busInstance.setMsgType(Constants.MSG_TYPE_POS);
		
		// 设置通话号码
		if (PMSUtils.isNotEmpty(busInstance.getDevice().getDevSimNum())) {
			busInstance.setCallPhone(busInstance.getDevice().getDevSimNum());
		}

		// 满载率
		String rate = busInstance.getAdditionalPassenerflowFullloadRate();
		busInstance.setPassenerflowFullloadRate(rate == null ? "0" : rate);
	}
	
}
